package com.galleryviewpager.h_loading;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class LoadingView extends View {
    private static final String TAG = "LoadingView";

    private int mWidth, mHeight;

    private Paint paint = new Paint();

    private int scaleYProgress = 100;

    private int mPathPercent = 0;

    private float circleHeightProgress = 0;

    private Future future, circleHeigthFuture;

    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        paint.setColor(Color.parseColor("#2ea4f2"));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(8f);
        paint.setAntiAlias(true);

        changeScaleYProgress();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
        } else {
            mWidth = 400;
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = MeasureSpec.getSize(heightMeasureSpec);
        } else {
            mHeight = 400;
        }
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Path path = new Path();
        RectF rectF = new RectF(5, 5, mWidth - 5, mHeight - 5);

        canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth / 2 - 5, paint);

        paint.setColor(Color.RED);
        canvas.drawLine(mWidth / 2, mHeight / 2 - 40 * scaleYProgress / 100, mWidth / 2, mHeight / 2 + 40 * scaleYProgress / 100, paint);

        path.moveTo(mWidth / 4, mHeight * 0.5f);
        double v = mPathPercent * 0.25 / 100;
        Log.e(TAG, "onDraw: " + v + " mPathPercent => " + mPathPercent + " mm=>" + mPathPercent / 100);
        path.lineTo(mWidth / 2, (float) (mHeight * (0.75 - v)));
        path.lineTo(mWidth * 0.75f, mHeight * 0.5f);
        canvas.drawPath(path, paint);
        if (mPathPercent == 100) {
            if (circleHeightProgress != 100) {
                canvas.drawCircle(mWidth / 2, mHeight / 2 * (1 - circleHeightProgress / 100f), 5, paint);
            }
            if (circleHeightProgress == 100) {
                Path path1 = new Path();
                path1.moveTo(mWidth / 4, mHeight * 0.5f);
                path1.lineTo(mWidth / 2, (float) (mHeight * (0.75f)));
                path1.lineTo(mWidth * 0.75f, mHeight * 0.5f);
                canvas.drawPath(path1, paint);
            }
        }
    }

    private void changeScaleYProgress() {
        final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();

        future = executorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {

                post(new Runnable() {
                    @Override
                    public void run() {

                        if (scaleYProgress < 20 && mPathPercent >= 100) {
                            future.cancel(true);

                            circleHeigthFuture = executorService.scheduleAtFixedRate(new Runnable() {
                                @Override
                                public void run() {
                                    post(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (circleHeightProgress >= 100) {
                                                circleHeigthFuture.cancel(true);
                                                return;
                                            }
                                            circleHeightProgress += 5;
                                            postInvalidate();
                                        }
                                    });
                                }
                            }, 1000, 200, TimeUnit.MILLISECONDS);
                            return;
                        }

                        if (scaleYProgress > 10) {
                            scaleYProgress -= 5;
                        }
                        if (mPathPercent < 100) {
                            mPathPercent += 5;
                        }
                        postInvalidate();
                    }
                });
            }
        }, 1000, 200, TimeUnit.MILLISECONDS);


    }


}
